package com.edu.cqr.web.demo.controller;

import com.edu.cqr.web.demo.config.ResultJson;
import com.edu.cqr.web.demo.dto.FreeRoomDTO;
import com.edu.cqr.web.demo.dto.RoomOrderDTO;
import com.edu.cqr.web.demo.service.RoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/room_order")
@Api(tags = "订房管理")
public class RoomOderConstroller {
    @Resource
    private RoomService roomService;

    @GetMapping("/get_free_room")
    @ApiOperation("查询客房剩余信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "checkInDate", value = "入住时间", dataType = "string", required = true),
            @ApiImplicitParam(name = "checkOutDate", value = "离开时间", dataType = "string", required = true)
    })
    @RequiresAuthentication
    public ResultJson getFreeRoom(String checkInDate, String checkOutDate) {
        try {
            List<FreeRoomDTO> freeRoom = roomService.findFreeRoomByMinFreeCount(checkInDate, checkOutDate);
            return new ResultJson("200", "查询成功!", freeRoom);
        } catch (Exception e) {
            return new ResultJson("400", "查询失败!", null);
        }
    }

    @PostMapping("/reserver_room")
    @ApiOperation("订房")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "鉴权Token", dataType = "string", required = true, paramType = "header"),
    })
    @RequiresAuthentication
    public ResultJson reserverRoom(@RequestBody RoomOrderDTO roomOrderDTO) {
        try {
            Long roomOrderNum = roomService.reserveRoom(roomOrderDTO);
            return new ResultJson("200", "创建订单成功!", "您的订单号为：" + roomOrderNum);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultJson("400", "创建订单失败!", null);
        }
    }
}
